package com.you.redis.sentinel;

import com.you.redis.util.JedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
 * jedis sentinel utils工具类
 */
public class JedisSentinelUtils {

    public static JedisSentinelUtils jedisSentinelUtils;

    public static JedisSentinelPool jedisSentinelPool = null;

    private JedisSentinelUtils() throws Exception {
        if (jedisSentinelUtils != null) {
            if (jedisSentinelUtils != null) {
                throw new Exception("不允许new JedisSentinelUtils 实例对象！");
            }
        }
        Set<String> sentinels = new HashSet<String>();
        String sentinelHost1 = "111.229.103.70:26379";
        String sentinelHost2 = "106.54.215.80:26379";
        String sentinelHost3 = "112.124.46.106:26379";
        sentinels.add(sentinelHost1);
        sentinels.add(sentinelHost2);
        sentinels.add(sentinelHost3);
        String masterName = "mymaster";
        String password = "newFintech@123";
        jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, password);
    }

    public static JedisSentinelUtils getInstance() throws Exception {
        if (jedisSentinelUtils == null) {
            synchronized (JedisSentinelUtils.class) {
                if (jedisSentinelUtils == null) {
                    jedisSentinelUtils = new JedisSentinelUtils();
                }
            }
        }
        return jedisSentinelUtils;
    }

    public static JedisSentinelPool getJedisSentinelPool() {
        return jedisSentinelPool;
    }
}
